dprint _werc/profile

enable_app profile

fn profile_init {
	profile_set_handler
	profile_process_post
}

fn profile_set_handler {
	switch($req_path) {
	case */profile/
		handler_body_main = profile_homepage
		post_arg_key=profile
		handler_body_minor = profile_slideshow
	case */edit
		handler_body_main = edit_profile
	case */map
			; # leave it to tpl
	case */logged_in
		if(~ $#profile_uri 0)
			handler_body_main = profile_create_form
		if not
			handler_body_main = credentials_form
	case *
		set_node_user_from_uripart 2
		handler_body_main = user_profile
	#	post_arg_key=profile
		handler_body_minor = profile_slideshow
	}
}

fn profile_uploaded_pic {
	determine_owner
	rm -rf $datastore/$owner/pix/profile.*
	mkdir -p $datastore/$owner/pix/
	cp $werc_root/$sitedir/imgs/noprofilepic.jpg $datastore/$owner/pix/profile.thumb.jpeg
	if(~ $#post_arg_data_filename 0) { # not a file, might be a uri
		# if this is a uri add it to the queue
		if(echo $post_arg_uri | grep -is '^https?://.+\..+') {
			handle_uri uploads
		}
		if not {
			push_error '<p class="error">Pictures only</p>'
		}
	}
	if not { # is a file upload, title it and move it to the right place
		ifs=$nl$spc
		info = `{$bin/docinfo $upload_dir/$post_arg_data_filename}
		sec=$info(1)
		if(~ $sec pix) {
			safetitle = profile
			handle_file $upload_dir/$post_arg_data_filename
		}
		if not {
			push_error '<p class="error">Pictures only</p>'
		}
	}
}

fn profile_process_post {
	if(~ $REQUEST_METHOD POST) {
		switch($post_arg_formname) {
		case edit_profile
			profile_save
			post_redirect /profile/
		case profile_create
			profile_create
			post_redirect /profile/
		case profile_pic
			dprint UPLOADED_PIC
			profile_uploaded_pic
		case upload
			switch($post_arg_hint) {
			case library
				handle_upload library
				post_redirect /profile/
			case pix
				handle_upload pix
				post_redirect /profile/
			}
		case *
			dprint 'unknown formname '$post_arg_formname' in _werc/profile'
		}
	}
}

fn profiles_page {
	echo profiles_page
}

fn edit_profile_link {
	if(! ~ $#logged_in 0) {
		echo '<p><a href="/profile/'$profile_uri^'/edit">Edit your profile</a></p>'
		echo '<form action="/profile/'$profile_uri'/" enctype="multipart/form-data" method="POST"><input type="hidden" name="formname" value="profile_pic"><p><label>Profile picture<br><input name="data" type="file"></label><br>
<label>or URI http://.. <input name="uri" type="text" value=""></label></p>
<input type="submit" value="Save"></form>'
	}
}

fn profile_layout {
	echo '<div class="profile">'
	u=`{profile_uri_from_hash $1}
	profile_open $u `{cat_as_html $datastore/$1/profile.name}
	if(test -s $datastore/$1/profile.md)
		md_handler $datastore/$1/profile.md
	profile_close
	edit_profile_link
	add_node p `{link -href /blog/new_post New Blog Post}
	profile_public_links $u
	echo '</div>'
}
fn user_profile {
	if(~ $logged_in $node_hash) {
		profile_homepage
	} 
	if not {
		profile_layout $node_hash
	}
}

fn profile_open {
	u=$1
	shift
	ifs=''
	echo '<table><colgroup><col width="105"><col width="*"></colgroup><tr valign="top"><td>'
	img -src /pix/$u/profile.thumb.jpeg -alt ':-)' -title $"*
	echo '</td><td>'
	add_node h1 $"*
}

fn profile_close {
	echo '</td></tr></table>'
}

fn edit_profile {
	profile_open $profile_uri $profile_hname
	echo '<p>We use <a href="http://en.wikipedia.org/wiki/Markdown" target="_blank">Markdown Syntax</a></p>'
	echo -n '<form action="/profile/'$profile_uri'/" method="POST"><input type="hidden" name="formname" value="edit_profile"><textarea name="profile" id="editprofile">'
	cat_as_html $datastore/$logged_in/profile.md
	echo '</textarea><p align="right"><input type="submit" value="Save"></p></form>'
	profile_close
}

fn profile_save {
	@ {
		mkdir -p $datastore/$logged_in
		if(cd $datastore/$logged_in) {
			purify $post_arg_profile> profile.md.new
			if(test -s profile.md.new) {
				mv profile.md profile.md.old >[2] /dev/null
				mv profile.md.new profile.md
				html2text < profile.md | $bin/indexer -u profile $logged_in profile > /dev/null
			}
		}
	}	
}

fn can_add_profile {
	if(test -s $datastore/$logged_in/profile.name) {
		push_error 'Sorry you have no profile slots left'
		status = 'No slots'
	}
	if not
		status = ''
}

fn profile_create_defaults { # hash name
	if(~ $#2 0) 
		status='No name supplied'
	if not {
		n=`{safename $2}
		if(~ $#n 0)
			status='Invalid profile name, try some different characters.'
		if not {
			if(test -e $datastore/*/$n.openid_hash)
				status = 'Sorry that profile name is taken'
			if not {
				mkdir -p $datastore/$1
				@ {
					if(cd $datastore/$1) {
						rm  -f *.openid_hash
						echo -n $2 > profile.name
						echo -n $n > profile.uri
						echo -n $1 > $n.openid_hash
					}
				}
				status = ''
			}
		}
	}
}

fn profile_create {
	can_add_profile && profile_create_defaults $logged_in $post_arg_profilename && user_init 
}

fn profile_create_form {
	if(! ~ $#errors 0)
		add_node p -class error `{pop_errors}
	if(~ $#logged_in 0 ) {
		credentials_form
	}
	if not {
		echo -n '<form action="/profile/" method="POST"><input type="hidden" name="formname" value="profile_create"><label>Create a profile called : [a-Z, 0-9 & space] <input type="text" name="profilename"><label><input type="submit" value="Create"></form>'
	}
}

fn profile_public_links {
	link -href '/pix/'$1'/' '<img src="/imgs/quadb.gif" alt="gallery" border="0"> gallery'
	link -href '/library/'$1'/' '<img src="/imgs/quadb.gif" alt="gallery" border="0"> library'
	link -href '/feeds/'$1'/' '<img src="/imgs/quadb.gif" alt="gallery" border="0"> feeds'
	link -href '/links/'$1'/' '<img src="/imgs/quadb.gif" alt="gallery" border="0"> links'
}

fn profile_homepage {
	if(~ $#profile_uri 0) {
		profile_create_form
	}
	if not {
		profile_layout $id
	}
}

fn profile_links { # no need
	if(! ~ $#logged_in 0 && ! ~ $#profile_uri 0) {
		edit_profile_link
		echo '<p><a href="'$profile_uri'/library/">Library</a> - share essays and interesting papers</p>'
		echo '<p><a href="'$profile_uri'/pix/">Pictures</a> - share your collection of photos</p>'
		echo '<p><a href="'$profile_uri'/feeds/">News Feeds</a> - keep tabs on external sites, Delicious or Flickr for instance</p>'
	}
}

fn profile_search_result {
	ifs=$tab b=`{cat}
	u= `{profile_uri_from_hash $b(3)}
	h= `{profile_name_from_hash_as_html $b(3)}
	echo '<tr valign="top"><td><img src="/imgs/fi.gif" alt="FI profile"></td></td><td colspan="2"><a href="/profile/'$u'">'$h'</a></td>'
	last_changed $datastore/$u/profile.md | sed 's/ .*//'
	echo -n '<td>' ^$b(4) ^'</td></tr>'
}

fn profile_slideshow {
	get_to_post
	if(~ $#post_arg_key 0) {
		if(~ $#node_uri 0)
			slideshow
		if not
			slideshow -u $node_uri
	}
	if not {
		if(~ $#node_uri 0)
			slideshow -k $post_arg_key
		if not
			slideshow -u $node_uri -k $post_arg_key
	}
}

fn  {

}
